package com.atguigu.view;

import com.atguigu.service.ChatService;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;

public class ChatView {
    private static Scanner input = new Scanner(System.in);

    public static void main(String[] args) throws IOException {
        boolean flag = true;
        while (flag) {
            System.out.println("================欢迎使用谷语聊天工具====================");
            System.out.println("\t\t\t\t1、查看历史聊天记录");
            System.out.println("\t\t\t\t2、开启新对话");
            System.out.println("\t\t\t\t3、退出");
            System.out.print("\t\t\t\t请选择：");
            int select = input.nextInt();
            input.nextLine();//读取整数后面的回车符
            switch (select) {
                case 1 -> history();
                case 2 -> chat();
                case 3 -> {
                    ChatService.saveChatRecords();
                    flag = false;
                }
            }
        }
    }

    public static void history() throws FileNotFoundException {
        //读取所有聊天记录的文件的列表
        ArrayList<File> files = ChatService.listAllHistories();
        for(int i=0; i<files.size(); i++){
            System.out.println((i+1) + "."  + files.get(i).getName());
        }

        //选择你要哪个聊天记录
        System.out.print("请选择你要查看哪个文件（填写编号）：");
        int id = input.nextInt();
        input.nextLine();

        //显示某个聊天记录的文件内容
        File file = files.get(id-1);
        Scanner scanner = new Scanner(file);
        while(scanner.hasNextLine()){
            String line = scanner.nextLine();
            System.out.println(line);
        }
        System.out.println();
        System.out.println();

        System.out.print("是否基于当前聊天记录继续聊（Y：继续，N：返回主菜单）");
        String confirm = input.nextLine();
        if("N".equalsIgnoreCase(confirm)){
            return;
        }else{
            //....
            //把之前的聊天记录生成一个摘要
            //开启新对话
        }
    }
    //聊天模块
    public static void chat() throws IOException {
        while(true){
            System.out.print("请输入你要问的问题：");
            String question = input.nextLine().trim();

            if("bye".equalsIgnoreCase(question)){
                System.out.println("谢谢使用谷语AI聊天程序！");
                break;
            }

            if(question.isBlank()){
                System.out.println("提问不能为空！");
                continue;
            }

            String answer = ChatService.chat(question);
            System.out.println("AI回答：" + answer);
        }
    }
}
